﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class StudentInExam : System.Web.UI.Page
    {
        static int examid = 0;
        static int studentid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (examid != 0)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myExam = from ex in db.ExamInfo
                                 where ex.ExamID == examid
                                 select ex.ExamName;
                    foreach (var exam in myExam)
                    {
                        DropDownField1.Text = exam.ToString();
                    }
                    var mySIE = from c in db.StudentInExam
                                where c.ExamID == examid
                                select new { StudentID = c.StudentID, StudentCode = c.StudentInfo.StudentCode, StudentName = c.StudentInfo.StudentName, ExamID = c.ExamID, Score = c.Score };
                    this.Store2.DataSource = mySIE;
                    this.Store2.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExam = from ex in db.ExamInfo
                             where ex.ExamID == examid
                             select ex.ExamName;
                foreach (var exam in myExam)
                {
                    DropDownField1.Text = exam.ToString();
                }
                var mySIE = from c in db.StudentInExam
                            where c.ExamID == examid
                            select new { StudentID = c.StudentID, StudentCode = c.StudentInfo.StudentCode, StudentName = c.StudentInfo.StudentName, ExamID = c.ExamID, Score = c.Score };
                this.Store2.DataSource = mySIE;
                this.Store2.DataBind();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if(examid==0)
            {
                 X.Msg.Alert("提示信息", String.Format("请选择一门考试")).Show();
            }
            else
            {
                Score.Text = "0";
                this.FormWindow.Show();
                Button7.Show();
                Button8.Hide();
            }
        }

        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            DropDownField3.Hide();
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    studentid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newsie = from s in db.StudentInExam
                                   where s.StudentID==studentid
                                   select s;
                    ExamAnalysis.Model.StudentInExam news = newsie.First();
                    Score.Text= news.Score.ToString();
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button7.Hide();
                Button8.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个学生").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    studentid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newsie = from s in db.StudentInExam
                                 where s.StudentID == studentid && s.ExamID == examid
                                 select s;
                    ExamAnalysis.Model.StudentInExam news = newsie.First();
                    db.StudentInExam.DeleteOnSubmit(news);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    studentid = 0;
                    Response.Redirect("StudentInExam.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个学生").Show();
            }
        }

        protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
               studentid  = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var theStudent = from c in db.StudentInfo
                              where c.StudentID==studentid
                              select c.StudentName;
                foreach (var student in theStudent)
                {
                    DropDownField3.Text = student.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (DropDownField3.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个学生")).Show();
            }
            else
            {
                ExamAnalysis.Model.StudentInExam newsie = new ExamAnalysis.Model.StudentInExam();
                newsie.Score = Convert.ToDecimal(Score.Text);
                newsie.StudentID = studentid;
                newsie.ExamID = examid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newsie2 = from s in db.StudentInExam
                                 where s.StudentID == newsie.StudentID && s.ExamID == newsie.ExamID
                                 select s;
                    if (newsie2.Count() != 0)
                    {
                        X.Msg.Alert("提示信息", String.Format("该学生成绩已存在，请选择修改")).Show();
                    }
                    else
                    {
                        db.StudentInExam.InsertOnSubmit(newsie);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功添加成绩")).Show();
                        studentid = 0;
                        Response.Redirect("StudentInExam.aspx");
                    }
                }
            }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newsie = from s in db.StudentInExam
                             where s.StudentID == studentid && s.ExamID == examid
                             select s;
                ExamAnalysis.Model.StudentInExam news = newsie.First();
                news.ExamID = examid;
                news.StudentID = studentid;
                news.Score = Convert.ToDecimal(Score.Text);
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改成绩")).Show();
                studentid = 0;
                DropDownField3.Show();
            }
            Response.Redirect("StudentInExam.aspx");
        }
    }
}